import json
import os
import rich
import Tools.console
from time import sleep, time, localtime
from rich.markdown import Markdown
from Problem.problem import Problem
from Problem.question import Question
from Tools.console import print as p, input
from Tools.tools import flat_list
from Config.file import Restore, Filename

QUESTION_POSITION = "./public/data1.txt"
ANSWER_CACHE_PATH = "./public/ANSWER"


ROOT_PATH = os.getcwd()
data = []


def load_path(filename: str):
    # 获取到全部数据
    with open(os.path.join(ROOT_PATH, QUESTION_POSITION), 'r', encoding='utf-8') as fp:
        try:
            return json.loads(fp.read())
        except Exception as e:
            print(e)


data = flat_list(load_path(QUESTION_POSITION))

size = len(data)


p("By shulandmimi\n", style="#abcdef")

print("共有 %d 题" % size)
p(Markdown("# 开始进行配置"))


def config_mode():
    return input(
        Markdown("请选择模式:\r\n1. 随机(有重复) default\r\n2. 随机(无重复)\r\n3. 顺序\r\n4. 死记硬背"))


store = Restore()


def config_restore():
    global data
    store.apply()
    if len(store.store_files) == 0:
        return
    store.print(store.store_files)
    i = input("请选择进度恢复: default(no)")
    if i == "" or i == None or i == "0":
        return
    filename = store.find_by_key_list(i.split(' '))
    if filename is None:
        print("请输入正确的序号")
        return

    file = Filename()
    file.encode()
    filename.append(file)
    store.current_filename = file

    data = store.read(filename)


config_restore()

mode = config_mode()


print("\n开始答题...")
p("3")
sleep(1)
p("2")
sleep(1)
p("1")
sleep(1)

print("\n\n")

problem = Problem(data, int(mode), store)

problem.run()
